require("../script/gloabs/gloabs");
const Scene = require("../script/gmoon/baseScene");

cc.Class({
    extends: Scene,

    onLoad(){
        this.setAttr("_current_page" , null );
        cc.director.getPhysicsManager().enabled=true;
        this._lightNode = this.childNode("game > bluelight");

        $RES.use(
            "img-mask-black",
            "img-mask-white",

            "img-home-logo",
            "img-home-logobg",
            "img-home-logozsq",
            "img-home-logozsqST",
            "img-home-logozsqSD",
            "img-home-logotitle" ,
            "img-home-levelLogo",
            "img-home-beginBtn")
        .then(()=>{
            $ACTION.do("befor-game-enter" ) //调入相应资源 以及 相关初始化逻辑
            .then(()=>{
                this.setAttr("_topimg" , true);
                this.loadHomePage();
            });
        })

        $RES.use(
            "img-home-bottomr",
            "img-home-bottoml"
        ).then(()=>{
            this.playBottomAnimate();
        })
    },

    playBottomAnimate(){
        let ws = cc.winSize;
        let leftNode = $G.do("oneNode.get"),
            rightNode = $G.do("oneNode.get");
        $T.setSprite( leftNode , $RES.g("img-home-bottoml"));
        $T.setSprite( rightNode , $RES.g("img-home-bottomr"));
        leftNode.parent = rightNode.parent = this.childNode("backs") ;

        leftNode.setPosition(cc.v2(-800 , -ws.height/2+leftNode.height/2 ));
        rightNode.setPosition(cc.v2(800 , -ws.height/2+rightNode.height/2 ));

        cc.tween(leftNode)
        .to(.3 , {position: cc.v2( -ws.width/2+leftNode.width/2 , leftNode.y )} )
        .call(()=>{
            cc.tween(rightNode)
            .to(.3 , {position: cc.v2( ws.width/2-rightNode.width/2 , rightNode.y )} )
            .start();
        })
        .start();
    },

    loadHomePage(){
        const cur = this.getAttr("_current_page");
        if(cur != "home"){
            $ACTION.do("befor-page-change" , cur , "home")
            .then((flg)=>{
                if(flg){
                    this.emptyPage()
                    .then(()=>{
                        let home = this.child("home");
                        home.node.active = true;
                        home.loadPanel();
                        this.setAttr("_current_page" , "home" );
                    });
                }
            })
        }
    },

    loadGamePage(){
        const cur = this.getAttr("_current_page");
        if(cur != "game"){
            $ACTION.do("befor-page-change" , cur , "home")
            .then((flg)=>{
                if(flg){
                    this.emptyPage()
                    .then(()=>{
                        $Animate.do("cameraSharkeIn" , $G.get("cameraComponent") , [1.5  , 1] , [.8 , .2 ] )
                        .then(()=>{
                            let gameScr = this.child("game");
                            gameScr.node.active = true;
                            //播放game开始动画
                            gameScr.playGameLevel()
                            .then(()=>{})
                            
                            this.setAttr("_current_page" , "game" );
                        })
                    });
                }
            })
        }
    },

    emptyPage(){
        const cur = this.getAttr("_current_page");
        return new Promise(( res ,rej )=>{
            let home = this.child("home"),
                menu = this.child("menus"),
                game = this.child("game");
            if(!cur){
                res();
                home.node.active = false;
                game.node.active = false;
                menu.node.active = false;
            }else if(cur=="home"){
                game.node.active = false;
                home.unLoadPanel()
                .then(()=>{
                    res();
                });
            }else  if(cur=="game"){
                home.node.active = false;
                game.unLoadPanel()
                .then(()=>{
                    menu.unLoadPanel()
                    res();
                });
            }
        });
    },

    update( dt ){
        //执行update的hooks
        $HOOK && $HOOK.run( "update" , dt);
    }
});
